package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.userwallet.UserSelectBankCardActivity;
import com.stn.zjx.givingtreasure.bean.BankCard;
import com.stn.zjx.givingtreasure.event.BankCardEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class SelectBankCardItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<BankCard> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public UserSelectBankCardActivity context;

   public SelectBankCardItemListAdapter(UserSelectBankCardActivity context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_select_bank_card, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
                   IndexHolder ih= (IndexHolder) viewHolder;
       BankCard   dataBean= dataBeanList.get(position);
           ih.bank_card_name.setText(dataBean.getUserBankCardName());

       if(dataBean.getUserBankCardNo().length()>4){
           ih.bank_card_no.setText(context.getString(R.string.dialog_bank_card_modfiy_c2)+
                   dataBean.getUserBankCardNo().substring(dataBean.getUserBankCardNo().length()-4,dataBean.getUserBankCardNo().length())
                   +" "+dataBean.getUserBankCardType());
       }else{
           ih.bank_card_no.setText(context.getString(R.string.dialog_bank_card_modfiy_c2)+
                   dataBean.getUserBankCardNo()+" "+dataBean.getUserBankCardType());
       }
        if(dataBean.getUserBankCardId()==context.userBankCardId){
            ih.select_iv.setImageResource(R.drawable.language_radio_select);
        }else{
            ih.select_iv.setImageDrawable(null);
        }
      ih.rootView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              BankCardEvent event=new BankCardEvent();
              event.bankCard=dataBean;
              event.flag=BankCardEvent.Select_Bank_Card;
              EventBus.getDefault().post(event);
              context.finish();
          }
      });
   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<BankCard> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<BankCard> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void delete(int position){
        dataBeanList.remove(position);
        notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }




   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
          public TextView bank_card_name;
          public  TextView bank_card_no;
          public ImageView select_iv;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            bank_card_name=itemView.findViewById(R.id.bank_card_name);
            bank_card_no=itemView.findViewById(R.id.bank_card_no);
            select_iv=itemView.findViewById(R.id.select_iv);
        }
   }


}